package com.cyt.work.kwmanagement.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.work.kwmanagement.dao.KwManagementDao;
import com.cyt.work.kwmanagement.model.po.KwKmMessage;
import com.cyt.work.kwmanagement.model.po.KwManagement;
import com.cyt.work.kwmanagement.model.vo.KwKmMessageVo;
import com.cyt.work.kwmanagement.model.vo.KwManagementVo;
import com.cyt.work.kwmanagement.service.KwKmMessageService;
import com.cyt.work.kwmanagement.service.KwManagementService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 考务安排表-考务安排表Service层实现类
 *
 * @author han jun
 * @date 2019-11-26 11:15:26
 */
@Service("kwManagementService")
public class KwManagementServiceImpl extends BaseServiceImpl<KwManagement> implements KwManagementService {

    @Resource
    private KwManagementDao kwManagementDao;

    @Resource
    private KwKmMessageService kwKmMessageService;


    @Autowired
    public void setBaseDao(KwManagementDao kwManagementDao) {
        this.baseDao = kwManagementDao;
    }

    /**
     * 查询对象集合
     *
     * @param kwManagementVo 对象
     * @return List 对象集合
     * @author han jun
     * @date 2019-11-26 11:15:26
     */
    @Override
    public List<KwManagement> listExample(KwManagementVo kwManagementVo) {
        Example example = new Example(KwManagement.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (!StringUtils.isEmpty(kwManagementVo.getId())){
            criteria.andEqualTo("id", kwManagementVo.getId());
        }
        if (!StringUtils.isEmpty(kwManagementVo.getTitle())){
            criteria.andLike("title", "%" + kwManagementVo.getTitle()+ "%");
        }
        if (!StringUtils.isEmpty(kwManagementVo.getState())){
            criteria.andEqualTo("state", kwManagementVo.getState());
        }
        if (!StringUtils.isEmpty(kwManagementVo.getApplyId())){
            criteria.andEqualTo("applyId", kwManagementVo.getApplyId());
        }
        return kwManagementDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param kwManagementVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:15:26
     */
    @Override
    public ReturnResult page(KwManagementVo kwManagementVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<KwManagement> list = listExample(kwManagementVo);
        PageInfo<KwManagement> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param kwManagementVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:15:26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(KwManagementVo kwManagementVo, SysUser sysUser) {
        if (StringUtils.isBlank(kwManagementVo.getId())) {
            KwManagement kwManagement = new KwManagement(sysUser);
            kwManagement.setApplyId(kwManagementVo.getApplyId());
            kwManagement.setTitle(kwManagementVo.getTitle());
            kwManagement.setKsType(kwManagementVo.getKsType());
            kwManagement.setStartTime(DateUtil.parseDateTime(kwManagementVo.getStartTime()+":00"));
            kwManagement.setEndTime(DateUtil.parseDateTime(kwManagementVo.getEndTime()+":00"));
            kwManagement.setAddress(kwManagementVo.getAddress());
            kwManagement.setRemark(kwManagementVo.getRemark());
            kwManagement.setState(kwManagementVo.getState());
            kwManagement.setStep(kwManagementVo.getStep());
            kwManagementDao.insert(kwManagement);
            handleKwKmMessage(kwManagement.getId(),kwManagement.getApplyId(),kwManagementVo.getKmMessageJson(), sysUser);

            return new ReturnResult(kwManagement.getId());
        } else {
            KwManagement kwManagement = kwManagementDao.selectByPrimaryKey(kwManagementVo.getId());
            kwManagement.setApplyId(kwManagementVo.getApplyId());
            kwManagement.setTitle(kwManagementVo.getTitle());
            kwManagement.setKsType(kwManagementVo.getKsType());
            kwManagement.setStartTime(DateUtil.parseDateTime(kwManagementVo.getStartTime()+":00"));
            kwManagement.setEndTime(DateUtil.parseDateTime(kwManagementVo.getEndTime()+":00"));
            kwManagement.setAddress(kwManagementVo.getAddress());
            kwManagement.setRemark(kwManagementVo.getRemark());
            kwManagement.setState(kwManagementVo.getState());
            kwManagement.setStep(kwManagementVo.getStep());
            kwManagement.setUpdateTime(new Date());
            kwManagement.setUpdateUser(sysUser.getId());
            kwManagementDao.updateByPrimaryKey(kwManagement);
            handleKwKmMessage(kwManagement.getId(),kwManagement.getApplyId(), kwManagementVo.getKmMessageJson(), sysUser);
            return new ReturnResult(kwManagement.getId());
        }
    }


    /**
     *  更新当前的步骤
     *
     * @param kwManagementVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:15:26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdateStep(KwManagementVo kwManagementVo, SysUser sysUser){
        KwManagement kwManagement = kwManagementDao.selectByPrimaryKey(kwManagementVo.getId());
        if (kwManagement==null){
            return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_UPDATE);
        }else {
            if (StringUtils.isNotBlank(kwManagementVo.getStep())){
                kwManagement.setStep(kwManagementVo.getStep());
            }
            if (StringUtils.isNotBlank(kwManagementVo.getState())){
                kwManagement.setState(kwManagementVo.getState());
            }
            kwManagement.setUpdateUser(sysUser.getId());
            kwManagementDao.updateByPrimaryKey(kwManagement);
            kwManagement.setUpdateTime(new Date());
            return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_UPDATE);
        }

    }


    /**
     * 保存考试科目信息
     *
     * @param ksId 考试id
     * @param kwKmMessageJson 科目信息
     * @param sysUser 用户信息
     * @return
     * @author Jun Han
     * @date 2019/12/2 17:15
     */
    private void handleKwKmMessage(String ksId,String applyId ,String kwKmMessageJson, SysUser sysUser) {
        KwKmMessageVo kwKmMessageVo = new KwKmMessageVo();
        kwKmMessageVo.setKsId(ksId);
        kwKmMessageService.removeAll(kwKmMessageVo, sysUser);
        List<KwKmMessageVo> kmMessageDTOList = JSON.parseArray(kwKmMessageJson, KwKmMessageVo.class);
        for (int i = 0; i < kmMessageDTOList.size(); i++) {
            kmMessageDTOList.get(i).setKsId(ksId);
            kmMessageDTOList.get(i).setApplyId(applyId);
            kwKmMessageService.addOrUpdate(kmMessageDTOList.get(i), sysUser);
        }
    }


    /**
     * 根据条件逻辑删除
     *
     * @param kwManagementVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:15:26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(KwManagementVo kwManagementVo, SysUser sysUser) {
        //要修改的字段
        KwManagement kwManagement = new KwManagement();
        kwManagement.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        kwManagement.setUpdateUser(sysUser.getId());
        kwManagement.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(KwManagement.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwManagementVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwManagementVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        kwManagementDao.updateByExampleSelective(kwManagement, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param kwManagementVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:15:26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(KwManagementVo kwManagementVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(KwManagement.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwManagementVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwManagementVo.getId().split(",")));
        }
        kwManagementDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 单个对象查询
     *
     * @param kwManagementVo 接受参数对象
     * @return KwTeacherSetting
     * @author han jun
     * @date 2019-11-26 11:26:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public KwManagementVo findOneVo(KwManagementVo kwManagementVo){
        //时间需要转化
        KwManagementVo kwManagementVo1 =  kwManagementDao.selectByCondition(kwManagementVo);
        KwKmMessageVo kwKmMessageVo = new KwKmMessageVo();
        kwKmMessageVo.setKsId(kwManagementVo1.getId());
        kwKmMessageVo.setApplyId(kwManagementVo1.getApplyId());
        kwManagementVo1.setStartTime(kwManagementVo1.getStartTime().substring(0,16));
        kwManagementVo1.setEndTime(kwManagementVo1.getEndTime().substring(0,16));
        List<KwKmMessage> list = kwKmMessageService.listExample(kwKmMessageVo);
        List<KwKmMessageVo> listVo = new ArrayList<>();
        for (KwKmMessage kwKmMessage:list){
            KwKmMessageVo newkwmessgeVo = new KwKmMessageVo();
            newkwmessgeVo.setKmmc(kwKmMessage.getKmmc());
            newkwmessgeVo.setBeginTime(DateUtil.format(kwKmMessage.getBeginTime(),"yyyy-MM-dd HH:mm"));
            newkwmessgeVo.setEndTime(DateUtil.format(kwKmMessage.getEndTime(),"yyyy-MM-dd HH:mm"));
            listVo.add(newkwmessgeVo);

        }
        kwManagementVo1.setKmMessageVOList(listVo);
        return kwManagementVo1;

    }
}